//
//  WQOAuthProgressView.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQOAuthProgressView: UIProgressView {

    // MARK: - ************属性
    var timer: NSTimer?
    var isAnim: Bool = true
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 50, width: SCREENSIZE.width, height: 5))
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - ************界面设置
    func setupUI() {
        
        backgroundColor = UIColor.greenColor()
    }
    
    // MARK: - ************开始
    func startAnim() {
        self.progress = 0
        self.hidden = false
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: #selector(WQOAuthProgressView.valueChanged), userInfo: nil, repeats: true)
    }
    
    // MARK: - ************结束
    func endAnim() {
        isAnim = false
    }
    
    // MARK: - ************进度变化
    func valueChanged() {
        
        if isAnim {
            self.progress += 0.05
            
            if self.progress >= 0.95 {
                self.progress = 0.95
            } 
        } else {
            if self.progress == 1 {
                self.hidden = true
                timer?.invalidate()
            } else {
                self.progress += 0.05
            }
        }
    }
    
    
    // MARK: - ************移除
    func removeProgress() {
        removeFromSuperview()
    }
    
    
    // MARK: - ************移除定时器
    deinit{
        timer?.invalidate()
    }
    
    
    
    
}
